package com.ql.gamelogm.service.impl;

import com.ql.gamelogm.dao.MailDao;
import com.ql.gamelogm.entities.Log;
import com.ql.gamelogm.entities.Mail;
import com.ql.gamelogm.entities.RedisCache;
import com.ql.gamelogm.service.MailService;
import com.ql.gamelogm.utils.List2PageUtil;
import com.ql.gamelogm.utils.MailUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class MailServiceImpl  implements MailService {
    private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class);

    @Autowired
    private MailDao mailDao;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private List2PageUtil list2PageUtil;
    @Autowired
    private MailUtils mailUtils;
    @Autowired
    private RedisCache redisCache;
    @Value("${spring.mail.from}")
    private String from;
    @Override
    public boolean SendMail(String to, String subject, String content) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = null;
        try {
            helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setTo(to);
            helper.setText(content, true);
            mailSender.send(message);
            //日志信息
            LOG.info("邮件已经发送。");
            return true;
        } catch (MessagingException e) {
            LOG.error("发送邮件时发生异常！", e);
            return false;
        }

    }

    @Override
    public void SaveMailSended(Mail mail) {
        mailDao.SaveMailSended(mail);
    }

    @Override
    public void SaveMailInBox(Mail mail) {
        mailDao.SaveMailInBox(mail);
    }

    @Override
    public boolean SaveMailDeleted(Mail mail) {
        return mailDao.SaveMailDeleted(mail);
    }

    @Override
    public void updateMailState(int id) {
        mailDao.updateMailState(id);

    }

    @Override
    public int DeleteMailInBoxById(int id) {

        return mailDao.DeleteMailInBoxById(id);

    }


    @Override
    public int DeleteMailSendedById(int id) {
        return mailDao.DeleteMailSendedById(id);
    }


    @Override
    public List<Mail> findAllMailsInBox() { return mailDao.findAllMailsInBox(); }

    @Override
    public List<Mail> findAllMailsSended() {
        return mailDao.findAllMailsSended();
    }

    @Override
    public List<Mail> findAllMailsDeleted() {
        return mailDao.findAllMailsDeleted();
    }

    @Override
    public List<Mail> findMailInBoxById( int id) {
        return mailDao.findMailInBoxById(id);
    }


    @Override
    public List<Mail> findMailSendedById(int id) {
        return mailDao.findMailSendedById(id);
    }

    @Override
    public List<Mail> findMailDeletedById( int id) {
        return mailDao.findMailDeletedById(id);
    }

    @Override
    public List<Mail> findAllMailsInBoxBySendTo(String sendto) {
        return mailDao.findAllMailsInBoxBySendTo(sendto);
    }

    @Override
    public List<Mail> findAllMailsDeletedBySendTo(String sendto) {
        return mailDao.findAllMailsDeletedBySendTo(sendto);
    }

    @Override
    public Page<Mail> getMailsInBoxList(int pageNum, int pageSize) throws Exception {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        //从163邮箱去取数据
        mailUtils.receive();
        //重新获取，判断是否需要更新数据
        redisCache.setMailInBoxRedis();
        List<Mail> mailList=redisCache.getMailInBoxRedis();
        return list2PageUtil.listConvertToPage(mailList,pageable);
    }

    @Override
    public Page<Mail> getMailsSendedList(int pageNum, int pageSize) {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<Mail> mailList=redisCache.getMailSendedRedis();
        return list2PageUtil.listConvertToPage(mailList,pageable);
    }

    @Override
    public Page<Mail> getMailsDeletedList(int pageNum, int pageSize) {
        Pageable pageable= PageRequest.of(pageNum,pageSize);
        List<Mail> mailList=redisCache.getMailDeletedRedis();
        return list2PageUtil.listConvertToPage(mailList,pageable);
    }


}
